#include "FileEnumJob.h"

LPCRITICAL_SECTION CFileEnumJob::m_pCS = NULL;
long	CFileEnumJob::m_lCounter = 0;
BOOL	CFileEnumJob::m_bStop	=  FALSE;

CFileEnumJob::CFileEnumJob(CListBoxEx* pFileList,wstring strPath,list<wstring> *pLstPath,HANDLE hEvent,vector<wstring>*	pVeFilter)
{
	m_pFileList =	pFileList;
	m_strPath	=	strPath;
	m_pLstPath	=	pLstPath;
	m_hEvent	=	hEvent;
	m_pVeFilter	=	pVeFilter;

	InterlockedIncrement(&CFileEnumJob::m_lCounter);

	if(NULL == CFileEnumJob::m_pCS)
	{
		CFileEnumJob::m_pCS = new CRITICAL_SECTION;
		InitializeCriticalSection(CFileEnumJob::m_pCS);
	}
}

CFileEnumJob::~CFileEnumJob(void)
{
	InterlockedDecrement(&CFileEnumJob::m_lCounter);

	if(m_lCounter <= 0 && NULL != CFileEnumJob::m_pCS)
	{
		if(NULL != CFileEnumJob::m_pCS)
		{
			DeleteCriticalSection(CFileEnumJob::m_pCS);
			delete CFileEnumJob::m_pCS;
			CFileEnumJob::m_pCS = NULL;
		}
	}
}

void CFileEnumJob::Lock()
{
	if(NULL != CFileEnumJob::m_pCS)
	{
		EnterCriticalSection(CFileEnumJob::m_pCS);
	}
}

void CFileEnumJob::UnLock()
{
	if(NULL != CFileEnumJob::m_pCS)
	{
		LeaveCriticalSection(CFileEnumJob::m_pCS);
	}
}

void CFileEnumJob::Stop()
{
	m_bStop = TRUE;
}

void CFileEnumJob::DoWork(void* pThreadContext)
{
	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;

	if(!m_pVeFilter)
	{
		return;
	}

	if(!m_pVeFilter->size())
	{
		return;
	}

	m_strPath += L"\\";
	wstring strTmp = m_strPath;

	strTmp += L"*";
	hFind = FindFirstFile(strTmp.c_str(),&FindFileData);

	if(hFind != INVALID_HANDLE_VALUE)
	{
		wstring strExtension;
		int nCount = m_pVeFilter->size();
		int i;
		BOOL bFind = FALSE;

		do{
			if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM || FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
				{

				}
				else
				{
#ifndef UNDER_CE
					if(!_tcscmp(FindFileData.cFileName,L"."))
					{

					}
					else if(!_tcscmp(FindFileData.cFileName,L".."))
					{

					}
					else
#endif
					{
						Lock();
						m_pLstPath->push_front(m_strPath + FindFileData.cFileName);
						UnLock();
						SetEvent(m_hEvent);
					}
				}
			}
			else
			{
				if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_SYSTEM 	&& FindFileData.dwFileAttributes&FILE_ATTRIBUTE_HIDDEN)
				{
					continue;
				}

				CUtil::GetFileExtension(FindFileData.cFileName,strExtension);

				if(!strExtension.length())
				{
					continue;
				}

				for(i = 0; i< nCount; i++)
				{
					if(strExtension == m_pVeFilter->at(i))
					{
						bFind = TRUE;
						break;
					}
					bFind = FALSE;
				}

				if(bFind)
				{
					Lock();
					m_pFileList->InsertItem(m_strPath + FindFileData.cFileName,0);
					UnLock();
					bFind = FALSE;
				}
			}
		} while ( !m_bStop && FindNextFile(hFind,&FindFileData)) ;
	}

	FindClose(hFind);
}

void CFileEnumJob::Abort()
{
	//Lock();
	//m_pFileList->InsertItem(L"Thread job queen test Abort",0);
	//UnLock();
}